package com.bbdjt.weixin.web.action.interflow;


import org.apache.log4j.Logger;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.bbdjt.weixin.base.action.BaseAction;
import com.bbdjt.weixin.model.Interflow;
import com.bbdjt.weixin.model.InterflowQueryModel;
import com.bbdjt.weixin.service.InterflowService;
import com.bbdjt.weixin.web.action.leaveWord.LeaveWordAction;

/**
 * @项目名称: bbdjt
 * @类名称: InterflowAction
 * @类描述: Interflow表相关操作
 * @创建人: guoLiang
 * @创建时间: 2017-03-20 12:05:23
 * @修改人: 
 * @修改时间: 
 * @修改备注:
 * @version
 */
@Controller
public class InterflowAction extends BaseAction {
	
	@Autowired
	private InterflowService interflowService;

	private String page_list = "jsp/interflow/interflowindex";

	private String redirect_list = "redirect:r_interflow";
	
	private static Logger logger = Logger.getLogger(InterflowAction.class);
	
	// 分页查询
	@RequestMapping(value = "r_interflow")
	public ModelAndView getListPage(InterflowQueryModel interflow) {
		ModelAndView result = new ModelAndView(page_list);
		interflow.setOrderBy("id asc");
		List<Interflow> interflowList = interflowService.selectByInterflow(interflow);
		result.addObject("pageInfo", new PageInfo<Interflow>(interflowList));
		result.addObject("interflow", interflow);
		return result;
	}

	// 自定义分页查询 根据xml 分页查询
	@RequestMapping(value = "r_interflowXml")
	public ModelAndView getListPageXml(InterflowQueryModel interflow) {
		ModelAndView result = new ModelAndView(page_list);
		interflow.setOrderBy("id asc");
		PageInfo<InterflowQueryModel> pageInfo = interflowService.selectByInterflowQueryModel(interflow);
		List<InterflowQueryModel> interflowList = pageInfo.getList();
		result.addObject("pageInfo", new PageInfo<InterflowQueryModel>(interflowList));
		result.addObject("interflow", interflow);
		return result;
	}

	// 返回整个页面的 json 包括分页 也签 内容 标示 和 正文 内容 同上方法 只不过是返回json
	@RequestMapping(value = "r_interflowJson", produces = "application/json")
	public ModelAndView pageInfo(InterflowQueryModel interflow) {
		ModelAndView result = new ModelAndView();
		PageInfo<InterflowQueryModel> pageInfo = interflowService.selectByInterflowQueryModel(interflow);
		result.addObject("pageInfo", pageInfo);
		return result;
	}
	
	//ligerui table
	@ResponseBody
	@RequestMapping(value = "r_interflowLigerUi", produces="application/json;charset=UTF-8") 
	public String pageInfoLigerUi(InterflowQueryModel interflow) {
		interflow.setOrderBy(interflow.getSortname(),interflow.getSortorder());
		PageInfo<InterflowQueryModel> pageInfo = interflowService.selectByInterflowQueryModel(interflow);
		return super.toLigerUiSelectJson(pageInfo);
	}

	// 添加数据 添加或者修改数据页面 主键为id
	// 如果没有id 不查 如果有查询补上信息
	@RequestMapping(value = "v_interflow")
	public ModelAndView add(Interflow interflow) {
		ModelAndView result = new ModelAndView();
		if (interflow.getId() != null) {
			interflow = interflowService.selectByKey(interflow.getId());
		}
		result.addObject("interflow", interflow);
		return result;
	}

	// 如果有id就更新 没有 就插入
	@RequestMapping(value = "c_interflow")
	public ModelAndView save(Interflow interflow) {
		ModelAndView result = new ModelAndView(redirect_list);
		if (interflow.getId() != null) {
			interflowService.updateNotNull(interflow);
		} else {
			// 会保存null 值
			interflowService.insertSelective(interflow);
		}
		return result;
	}

	// 删除数据
	@RequestMapping("d_interflow")
	public String delete(Integer id) {
		interflowService.delete(id);
		return redirect_list;
	}
	
	//患者和医生交互
	@ResponseBody
	@RequestMapping(value = "addInterflow")
	public String addInterflow(Interflow interflow){
		if(interflow.getSyllabusId() != null && interflow.getUserId() != null && interflow.getMessage() != null){
			interflow.setCreateTime(new Date());
			interflowService.insertSelective(interflow);
			return super.writeOK(interflow);
		}else{
			logger.info("用户ID或发言内容或课程ID为空:" + interflow.getSyllabusId() + "," + interflow.getUserId() + "," + interflow.getMessage());
			return super.lackError();
		}
	}
	
	//医生患者查看留言
	@ResponseBody
	@RequestMapping(value = "interflowAllSyllabus" ,produces="application/json;charset=UTF-8")
	public String interflowAllSyllabus(Interflow interflow){
		if(interflow.getSyllabusId() != null && interflow.getUserType() != null){
			List<Interflow> interflowList = interflowService.interflowPatientSyllabus(interflow);
			return super.writeOK(interflowList);
		}else{
			logger.info("用户类型或课程ID为空:" + interflow.getSyllabusId() + "," + interflow.getUserType());
			return super.lackError();
		}
	}
}